package com.heima.article.listen;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleContentService;
import com.heima.common.constants.message.NewsUpOrDownConstants;
import com.heima.model.article.pojo.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author by jojo
 * @Date 2022/3/5
 * @Description
 */
@Component
@Slf4j
public class ArticleUpOrDownListener {

    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @RabbitListener(queuesToDeclare = @Queue(NewsUpOrDownConstants.NEWS_UP_FOR_ARTICLE_CONFIG_QUEUE))
    public void newsUp(String articleId) {
        log.info("接收到了自媒体上架的消息 articleId：{}", articleId);
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown, false)
                    .eq(ApArticleConfig::getArticleId, Long.valueOf(articleId)));
            log.info("文章修改上架成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文章修改上架失败 原因：{}",e.getMessage());
        }
    }

    @RabbitListener(queuesToDeclare = @Queue(NewsUpOrDownConstants.NEWS_DOWN_FOR_ARTICLE_CONFIG_QUEUE))
    public void newsDown(String articleId) {
        log.info("接收到了自媒体下架的消息 articleId：{}", articleId);
        try {
            apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                    .set(ApArticleConfig::getIsDown, true)
                    .eq(ApArticleConfig::getArticleId, Long.valueOf(articleId)));
            log.info("文章修改下架成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("文章修改下架失败 原因：{}",e.getMessage());
        }
    }
}
